<?

	require_once 'file.php';

	class Fmgr0v0r01_image extends Fmgr0v0r01_file implements Fmgr_image{
		
		var $media_class = 'Image';
		
		function set_functions(){
			static $a;
			if(!$a) $a = array_merge(parent::set_functions(), array(
				'x',
				'y',
				'max',
				'max_x',
				'max_y',
				'brightness',
				'contrast',
				'threshold',
				'negative',
				'grayscale',
				'flip_horizontal',
				'flip_vertical',
				'rotate',
				'crop'
			));
			return $a;	
		}
		
		function get_functions(){
			static $a;
			if(!$a) $a = array_merge(parent::get_functions(), array(
				'x',
				'y',
				'width',
				'height',
				'is_webimage',
				'img_tag',
				'thumb',
				'bigicon'
			));
			return $a;	
		}
		
		function summary_vars(){
			static $a;
			if(!$a) $a = array_merge_recursive(parent::summary_vars(), array(
				"1:" => array(
					'x',
					'y',
					'xtn',
					'bigicon'
				)
			));
			return $a;
		}
		
		// web_types are mime subtypes that display happily on web pages
		protected $web_types = 'jpeg,jpg,gif,png';
		protected $gd;	
			
		private function gd(){
			if(!is_null($this->gd)) return $this->gd;
			$this->gd = new gd($this->realpath());
			return $this->gd;
		}
		
		function icon($s=32){
			return (($t = $this->thumb()) && ($t = $t->get($s))) ? $t->uri() : 'image.png';
		}
		
		function bigicon(){
			return (($t = $this->thumb()) && ($t = $t->get(128))) ? $t->uri() : 'image.png';	
		}
			
####	get functions
		
		protected function read_size(){
			$d = getimagesize($this->realpath());
			$m = $this->meta();
			$m->x = $d[0];
			$m->y = $d[1];
			return $d;
		}
		
		function thumb(){
			if(!$this->allow_read || $this->is_draft() || $this->is_thumb() || !$this->exists()) return false;
			return $this->pkg->open('temp:/thumbs')->thumb($this);
		}
		
		function is_thumb(){
			return (substr($this->data->fmrp, 0, 11) == 'temp:/thumbs/');
		}
		
		function x(){
			return $this->get_dim('x');
		}
		
		function y(){
			return $this->get_dim('y');
		}
		
		function width(){
			return $this->get_dim('x');
		}
		
		function height(){
			return $this->get_dim('y');
		}
		
		protected function get_dim($k='x'){
			$m = $this->meta(1);
			if(!$v = $m->$k) $this->read_size();
			return ($v) ? $v : $m->$k;
		}
		
		function img_tag(){
			$tag = '<img src="'.$this->uri().'" width="'.$this->x().'" height="'.$this->y().'"';
			$tag .= ' alt="'.$this->data->description.'"';
			$tag.= ' title="'.$this->data->title.'"';
			$tag .= ' />';
			return $tag;
		}
		
		function tag(){
			return $this->img_tag();
		}
		
		function forced_xtn(){
			return $this->is_webimage() ? $this->xtn() : false;
		}
		
		function usurpa(){
			parent::usurpa();
			if($this->usurp_mode){
				$u = $this->get_usurpa();
				$this->data->href = $u->href();
				$this->data->uri = $u->uri();
			}
			return $this;
		}
		
####	bool functions

		function is_webimage(){
			return ($this->in_settings_array($this->mime_subtype(), 'web_types'));
		}
		
####	processing
		
		function set_x($x){
			$this->gd()->image_resize = true;
			$this->gd()->image_x = max($x, 1);
			return true;
		}
		
		function set_y($y){
			$this->gd()->image_resize = true;
			$this->gd()->image_y = max($y, 1);
			return true;
		}
		
		function set_max($n){
			if(!$n) return false;
			$this->gd()->image_resize = true;
			$x = $this->x();
			$y = $this->y();
			switch(true){
				case($x == $y):
					$this->gd()->image_x = $this->gd()->image_y = $n;
					return true;
				case($x > $y):
					$this->gd()->image_x = $n;
					$this->gd()->image_y = max(bcdiv($y, bcdiv($x, $n, 3)), 1);
					return true;
				default:
					$this->gd()->image_x = max(bcdiv($x, bcdiv($y, $n, 3)), 1);
					$this->gd()->image_y = $n;
					return true;	
			}
		}
		
		function set_max_x($n){
			if(!$n) return false;
			$x = $this->x();
			$y = $this->y();
			switch(true){
				case($x == $y):
					$this->gd()->image_resize = true;
					$this->gd()->image_x = $this->gd()->image_y = $n;
					return true;
				case($x > $n):
					$this->gd()->image_resize = true;
					$this->gd()->image_x = $n;
					$this->gd()->image_y = max(bcdiv($y, bcdiv($x, $n, 3)), 1);
					return true;
				default:
					return false;	
			}
		}
		
		function set_max_y($n){
			if(!$n) return false;
			$x = $this->x();
			$y = $this->y();
			switch(true){
				case($x == $y):
					$this->gd()->image_resize = true;
					$this->gd()->image_x = $this->gd()->image_y = $n;
					return true;
				case($y > $n):
					$this->gd()->image_resize = true;
					$this->gd()->image_x = max(bcdiv($x, bcdiv($y, $n, 3)), 1);
					$this->gd()->image_y = $n;
					return true;
				default:
					return false;	
			}
		}
		
		function set_brightness($n){
			$this->gd()->image_brightness = $n;
			return true;
		}
		
		function set_contrast($n){
			$this->gd()->image_contrast = $n;
			return true;
		}
		
		function set_threshold($n){
			$this->gd()->image_threshold = $n;
			return true;
		}
		
		function set_negative($n){
			$this->gd()->image_negative= $n;
			return true;
		}
		
		function set_grayscale($n){
			// Note british spelling in Colin's gd class
			$this->gd()->image_greyscale= $n;
			return true;
		}
		
		function set_rotate($n){
			$this->gd()->image_rotate= $n;
			return true;
		}
		
		function set_crop($n){
			$this->gd()->image_crop = $n;
			return true;
		}
		
		function set_resize($n){
			$this->gd()->image_resize = $n;
			return true;
		}
		
		// Colin got these wrong...		
		function set_flip_horizontal($n){
			$this->gd()->image_flip = 'v';
			return true;
		}
		
		function set_flip_vertical($n){
			$this->gd()->image_flip = 'h';
			return true;
		}

		function set_mime_subtype($m){
			if(!$this->in_settings_array($m, 'web_types')) return false;
			$this->gd()->image_convert = $m;
			return true;
		}
		
		protected function run_set_functions(){
			if(!$this->is_webimage()) $this->mime_subtype = 'png';
			parent::run_set_functions();
			$this->process();
		}
		
		protected function get_new(){
			if(!$this->is_webimage()){
				$this->gd()->image_convert = 'png';
				$this->process();
			}
			return parent::get_new();
		}
		
		function process(){
			if(!$this->gd) return;					
			$td = Cache::mktemp();
			ini_set('max_execution_time', 120);
			$this->gd->Process($td);
			if(!$this->gd->processed){
				error_log($this->gd->error);
				rmdir($td);
				return false;
			}
			$if = $this->gd->file_dst_pathname;
			unlink($this->realpath());
			$of = filename_increment($this->parent()->realpath(basename($if)));
			rename($if, $of);
			$bn = basename($of);
			$this->update_fmrp($this->parent()->child_fmrp($bn));
			rmdir($td);
			$this->read_size();
			$this->gd = null;
			$this->add_sanity_check('filesize');
			return true;
		}
		
		protected function destroy_row(){
			parent::destroy_row();
			$this->pkg->open('temp:/thumbs')->unthumb($this);
			return true;
		}
		
	}

?>
